package com.equipmentRent.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.equipmentRent.common.utils.StringUtils;
import com.equipmentRent.system.domain.Category;
import com.equipmentRent.system.domain.VO.ItemDetialsVo;
import com.equipmentRent.system.domain.dto.ItemDto;
import com.equipmentRent.system.domain.dto.QueryItemDto;
import com.equipmentRent.system.service.ICategoryService;
import javafx.scene.control.Tab;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.equipmentRent.common.annotation.Log;
import com.equipmentRent.common.enums.BusinessType;
import com.equipmentRent.system.domain.Item;
import com.equipmentRent.system.service.IItemService;
import com.equipmentRent.common.core.controller.BaseController;
import com.equipmentRent.common.core.domain.AjaxResult;
import com.equipmentRent.common.utils.poi.ExcelUtil;
import com.equipmentRent.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 租赁物品主Controller
 * 
 * @author zxl
 * @date 2025-05-17
 */
@Controller
@RequestMapping("/system/item")
public class ItemController extends BaseController
{
    private String prefix = "system/item";

    @Autowired
    private IItemService itemService;

    @Resource
    private ICategoryService categoryService;

    @RequiresPermissions("system:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询租赁物品主列表
     */
    @RequiresPermissions("system:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Item item)
    {
        startPage();
        List<Item> list = itemService.selectItemList(item);
        List<ItemDto> listDto = new ArrayList<>();
        list.forEach(it -> {
            Category category = categoryService.selectCategoryByCategoryId(it.getCategoryId());
            ItemDto itemDto = new ItemDto();
            BeanUtils.copyProperties(it,itemDto);
            itemDto.setCategoryName(Optional.ofNullable(category).map(Category::getCategoryName).orElse("--"));
            listDto.add(itemDto);
        });
        TableDataInfo dataTable = getDataTable(listDto);
        dataTable.setRows(listDto);
        dataTable.setTotal(listDto.size());
        return dataTable;
    }


//    todo 客户端器材列表接口
    @PostMapping("/getItemList")
    @ResponseBody
    public AjaxResult clientList(@RequestBody QueryItemDto dto)
    {
        Item item = new Item();
        if (StringUtils.isNotEmpty(dto.getCategoryId())){
            item.setCategoryId(Long.valueOf(dto.getCategoryId()));
        }
        if (StringUtils.isNotEmpty(dto.getItemName())){
            item.setName(dto.getItemName());
        }

        List<Item> list = itemService.selectItemList(item);
        List<ItemDto> listDto = new ArrayList<>();
        list.forEach(it -> {
            Category category = categoryService.selectCategoryByCategoryId(it.getCategoryId());
            ItemDto itemDto = new ItemDto();
            BeanUtils.copyProperties(it,itemDto);
            itemDto.setCategoryName(Optional.ofNullable(category).map(Category::getCategoryName).orElse("--"));
            listDto.add(itemDto);
        });
        return AjaxResult.success(listDto);
    }
//    todo 客户端器材详情接口
    @PostMapping("/getItemDetails")
    @ResponseBody
    public AjaxResult getItemDetails(@RequestBody String itemId){
        Item item = itemService.selectItemById(Long.valueOf(itemId));
        Category category = categoryService.selectCategoryByCategoryId(item.getCategoryId());
        ItemDetialsVo itemDetialsVo = new ItemDetialsVo();
        BeanUtils.copyProperties(item,itemDetialsVo);
        itemDetialsVo.setCategoryName(Optional.ofNullable(category).map(Category::getCategoryName).orElse("--"));
        return AjaxResult.success(itemDetialsVo);
    }
    /**
     * 导出租赁物品主列表
     */
    @RequiresPermissions("system:item:export")
    @Log(title = "租赁物品主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Item item)
    {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        return util.exportExcel(list, "租赁物品主数据");
    }

    /**
     * 新增租赁物品主
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("categoryList",categoryService.selectCategoryList(new Category()));
        return prefix + "/add";
    }

    /**
     * 新增保存租赁物品主
     */
    @RequiresPermissions("system:item:add")
    @Log(title = "租赁物品主", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Item item)
    {
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 修改租赁物品主
     */
    @RequiresPermissions("system:item:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Item item = itemService.selectItemById(id);
        mmap.put("item", item);
        mmap.put("categoryList",categoryService.selectCategoryList(new Category()));
        return prefix + "/edit";
    }

    /**
     * 修改保存租赁物品主
     */
    @RequiresPermissions("system:item:edit")
    @Log(title = "租赁物品主", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Item item)
    {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除租赁物品主
     */
    @RequiresPermissions("system:item:remove")
    @Log(title = "租赁物品主", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(itemService.softDeleteItemByIds(ids));
    }
}
